package com.example.demo.controller;

import java.util.Properties;


import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Properties;
import java.util.Random;

class Register {
        public String username;
        public String password1;
        public String password2;
        public String passNum;
    }

    @RestController
    public class RegisterController {
        @Autowired
        private UserService userService;

        public String passNum;

        @RequestMapping("/register")
        public Object userRegister(@RequestBody Register register) {
            HashMap<String, Object> map = new HashMap<>();
            User user = userService.selectByName(register.username);
            if (user != null) {
                map.put("message", "当前用户名已经存在了, 请更换!");
                return map;
            }
            System.out.println(passNum);
            System.out.println(register.passNum);
            if(!register.passNum.equals(passNum)){
                map.put("message", "验证码错误");
                return map;
            }
            User user1 = new User();
            user1.setUsername(register.username);
            user1.setPassword(register.password1);
            userService.addUser(user1);
            map.put("message","注册成功!");
            return map;
        }

        @RequestMapping("/sendEmail")
        public String sendEmail(String email){
// 收件人电子邮箱
            String to = email;

            // 发件人电子邮箱
            String from = "2669032800@qq.com";
// 指定发送邮件的主机为 smtp.qq.com
            String host = "smtp.qq.com";  //QQ 邮件服务器

            // 获取系统属性
            Properties properties = System.getProperties();

            // 设置邮件服务器
            properties.setProperty("mail.smtp.host", host);

            properties.put("mail.smtp.auth", "true");
            // 获取默认session对象
            Session session = Session.getDefaultInstance(properties,new Authenticator(){
                public PasswordAuthentication getPasswordAuthentication()
                {
                    return new PasswordAuthentication("2669032800@qq.com", "jcamgqqpsvtcdhic"); //发件人邮件用户名、授权码
                }
            });

            try{
                // 创建默认的 MimeMessage 对象
                MimeMessage message = new MimeMessage(session);

                // Set From: 头部头字段
                message.setFrom(new InternetAddress(from));

                // Set To: 头部头字段
                message.addRecipient(Message.RecipientType.TO,
                        new InternetAddress(to));

                // Set Subject: 头部头字段
                message.setSubject("验证码");

                String res = "";
                for (int i = 0; i < 4; i++) {
                    int n = new Random().nextInt(10);
                    res += n;
                }
                passNum = res;
                // 设置消息体
                message.setText("验证码为: " + res);

                // 发送消息
                Transport.send(message);
                return res;
            }catch (MessagingException mex) {
                mex.printStackTrace();
            }
            return null;
        }

    }
